//
//  ListenNewsViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "ListenNewsViewController.h"
#import "ZCSListenNewsModel.h"
#import "TYTabButtonPagerController.h"
#import "ZCSNewsTagsModel.h"
#import "ViewController.h"
#import "ZCSListenNewsRecommendViewController.h"
#import "ZCSListenNewsTypeViewController.h"

@interface ListenNewsViewController ()<TYPagerControllerDataSource>
@property(nonatomic, retain)ZCSListenNewsModel *newsModel;
@property (nonatomic, strong) TYTabButtonPagerController *pagerController;
@end

@implementation ListenNewsViewController
- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    _pagerController.view.frame = self.view.bounds;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.titleString;
    self.navigationController.navigationBar.hidden = NO;
    [self creatView];
    [self getDataWith:self.categoryId];
}
- (void)viewWillAppear:(BOOL)animated
{
    
}

- (void)creatView
{
    TYTabButtonPagerController *pagerController = [[TYTabButtonPagerController alloc]init];
    pagerController.dataSource = self;
    pagerController.adjustStatusBarHeight = YES;
    pagerController.barStyle = TYPagerBarStyleProgressView;
    pagerController.cellSpacing = 8;
    pagerController.progressWidth = 0;
    pagerController.view.frame = self.view.bounds;
    [self addChildViewController:pagerController];
    [self.view addSubview:pagerController.view];
    _pagerController = pagerController;
    
    
}

- (NSInteger)numberOfControllersInPagerController
{
    return self.newsModel.tagsArray.count + 1;
}

- (NSString *)pagerController:(TYPagerController *)pagerController titleForIndex:(NSInteger)index
{
    if (index == 0) {
        return @"推荐";
    }
    else
    {
        ZCSNewsTagsModel *tagModel = [self.newsModel.tagsArray objectAtIndex:index - 1];
        return tagModel.tname;
    }
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index
{
    if (index == 0) {
        ZCSListenNewsRecommendViewController *lnrvc = [[ZCSListenNewsRecommendViewController alloc] init];
        
        lnrvc.cId = self.categoryId;
        return lnrvc;
    }
    else
    {
        ZCSListenNewsTypeViewController *typeVC = [[ZCSListenNewsTypeViewController alloc] init];
        typeVC.caId = self.categoryId;
        ZCSNewsTagsModel *tagModel = [self.newsModel.tagsArray objectAtIndex:index - 1];
        typeVC.tapName = tagModel.tname;
        return typeVC;
    }
}

#pragma mark -- 推荐数据源
- (void)getDataWith:(NSInteger)cateId
{
    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=%ld&contentType=album&device=android&version=4.3.92", cateId];
    __weak ListenNewsViewController *nvc = self;
    [LTNetTool GetNetWithURL:string Body:nil Header:nil Response:0 Success:^(id result) {
        nvc.newsModel = [[ZCSListenNewsModel alloc] initWithDic:result];
        if (nvc.newsModel) {
           [nvc.pagerController reloadData];
        }
        
        
    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
